﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using libtcod;

namespace Threshold.MapGenerators
{
	class CaveGenerator : IMapGenerator
	{
		public Map Generate(int width, int height)
		{
			TCODNoise noise = new TCODNoise(2);
			TCODNoise floorNoise = new TCODNoise(2);
			float zoom = 3.0f;
			float octaves = 2.0f;

			float floatValue = 0.0f;
			int intValue = 0;

			TCODColor floorColor;
			TCODColor wallColor = new TCODColor(92, 64, 51);

			int floorCharacter = 32;
			int wallCharacter = 219;
			int displayCharacter;

			bool isTransparent = false;
			bool isWalkable = false;
			bool isExplored = false;

			float[] f = new float[2];

			Map map = new Map(width, height);

			for (int x = 0; x < map.Width; x++)
			{
				for (int y = 0; y < map.Height; y++)
				{
					// floor color
					f[0] = 5.0f * x / map.Width;
					f[1] = 5.0f * y / map.Height;
					floatValue = floorNoise.getPerlinTurbulence(f, octaves);
					intValue = (int)((floatValue + 1.0f) / 2.0f * 255);
					floorColor = new TCODColor(intValue / 4, intValue / 4, intValue / 4);

					// floor or wall?
					f[0] = zoom * x / map.Width;
					f[1] = zoom * y / map.Height;
					floatValue = noise.getPerlinBrownianMotion(f, octaves);
					if (floatValue >= 0.0f)
					{
						displayCharacter = wallCharacter;
						isTransparent = false;
						isWalkable = false;
					}
					else
					{
						displayCharacter = floorCharacter;
						isTransparent = true;
						isWalkable = true;
					}
					map.SetData(x, y, displayCharacter, wallColor, floorColor, isTransparent, isWalkable, isExplored);
				}
			}
			return map;
		}
	}
}
